name = 'dir';

function ro_ls(request)
{
    var argument = request.argument();
    var claimant = request.from();
    var rid = request.id();

    var badParamsError = RoIqStanza.createError(RoIqStanza.BadParameters, claimant, rid);

    if ( argument.documentElement().tagName() != "path" )
    {
        badParamsError.setErrorDescription("Argument must be <path>path to a directory</path>");
        return [FinishedWithError, badParamsError];
    }

    var path = argument.documentElement().text();

    if ( path == '' )
    {
        badParamsError.setErrorDescription("<path/> is empty");
        return [FinishedWithError, badParamsError];
    }

    var dir = new QDir(path);
    if ( ! dir.exists() )
    {
        badParamsError.setErrorDescription("path doesn't lead to a directory");
        return [FinishedWithError, badParamsError];
    }

    var entryInfoList = dir.entryInfoList(QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.Hidden | QDir.System));
    for ( var i = 0; i < entryInfoList.length; ++i )
    {
        var fi = entryInfoList[i];

        var datum = RoIqStanza.createDatum(claimant, rid, "fileinfo");
        datum.setBodyText(fi.fileName());

        if ( fi.isSymLink() )
            datum.setBodyAttribute("symlink", "true");

        if ( fi.isDir() )
            datum.setBodyAttribute("dir", "true");
        else
            datum.setBodyAttribute("size", fi.size());

        sendStanza(datum);
    }

    return [FinishedSuccessfully, XML("ok")];
}

ro_ls.example = '<path>some path</path>';
